<script>
    layui.use(['jquery', 'upload','layer'],function () {
        let $ = layui.jquery
        let upload = layui.upload
        let layer = layui.layer;

        //普通图片上传
        renderUploadPic('uploadPic','cover','uploadDemoView');
        function renderUploadPic(elem,cover,preview) {
            upload.render({
                elem: '#'+elem
                ,url: '{{ env('APP_URL').route("api.upload_file",null,false) }}'
                ,multiple: false
                ,accept: 'images'
                ,exts: 'jpg|jpeg|gif|png'
                ,size: '10240'
                ,acceptMime:'image/*'
                ,data:{"_token":"{{ csrf_token() }}"}
                ,before: function(){
                    layer.load();
                }
                ,done: function(res){
                    //如果上传失败
                    if(res.code === 200){
                        $("#"+cover).val(res.data.file_id);
                        $('#'+preview).removeClass('layui-hide');
                        $('#'+preview).removeClass('layui-hide');
                        $('#'+preview).find('img').attr('src','{{ env('APP_URL')}}' + res.data.link);
                        layer.closeAll('loading');
                        return layer.msg(res.msg,{icon: 6, shift: 6});
                    }
                    layer.closeAll('loading');
                    return layer.msg(res.msg, {icon: 5, shift: 6});
                }
                ,error: function(index, upload){
                    layer.closeAll('loading');
                }
            });
        }
    })
</script>
